<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回收站</title>
    <link rel="stylesheet" href="/static/crm/lib/layui-v2.6.3/css/layui.css" media="all">
</head>

<body>
    <script src="/static/crm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script type="text/html" id="toolbarDemo">

        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger layui-btn-sm layui-btn-disabled " lay-event="destroy" id="destroy"><i class="layui-icon layui-icon-delete"></i> 销毁</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger layui-btn-disabled " lay-event="restore" id="restore"><i class="layui-icon layui-anim layui-icon-refresh-3" data-anim="layui-anim-rotate"></i>还原</button>
        </div>
    </script>

    <table class="layui-hide layui-table" lay-data="{id: 'idTest'}" id="current" lay-filter="currentTableFilter">
    </table>
</body>

</html>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        let userTable = table.render({
            elem: '#current',
            url: 'recycle',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { type: "checkbox", width: 50, fixed: 'left' },
                { field: 'id', title: 'ID', sort: true, fixed: 'left' },
                { field: 'number', minWidth: '190', title: '订单编号' },
                { field: 'name', minWidth: '110', title: '订单名称' },
                { field: '', while: '80', title: '客户' },
                { field: 'money', minWidth: '110', title: '订单金额', sort: true },
                { field: 'deposit', minWidth: '110', title: '回款金额', sort: true },
                { field: '', Width: '80', title: '审批人', },
                { field: 'status', Width: '80', title: '状态', sort: true },
            ]],
            limits: [5, 10, 20, 25, 50, 100],
            limit: 5,
            page: true,
            skin: 'line',
            parseData: function (res) {
                var result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return { 'code': res.code, 'msg': res.msg, 'count': res.total, 'data': result };
            },
        });
        table.on('checkbox(currentTableFilter)', function (obj) {
            var data = obj.data;
            var _id = data['id'];
            if (obj.checked == true) {
                $("#destroy").removeClass("layui-btn-disabled")
                $("#restore").removeClass("layui-btn-disabled")
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'restore') {  // 监听还原操作
                        var checkStatus = table.checkStatus(obj.config.id),
                            data = checkStatus.data,
                            userId = "";
                            console.log(checkStatus)
                        //  //获取选中行数量，可作为是否有选中行的条件
                        if (data.length > 0) {
                            for (var i in data) {
                                userId += data[i].id + ",";
                            }
                            layer.confirm('确定还原选中的用户？', { icon: 3, title: '提示信息' }, function (index) {
                                $.ajax({
                                    url: "restore",
                                    type: "POST",
                                    data: { id: userId },
                                    success: function (msg) {
                                        if (msg.errcode == "1") {
                                            layer.msg('还原成功!', function () { time: 2000 })
                                            table.reload('current', {
                                                scrollPos: 'fixed'
                                            });
                                        } else {
                                            layer.msg("还原失败", function () { time: 2000 });
                                        }
                                    }
                                });
                            });
                        } else {
                            layer.msg("请选择需要还原的用户");  //如果未选择数据，则提示。
                        }
                    } else if (obj.event === 'destroy') {  // 销毁
                        var checkStatus = table.checkStatus(obj.config.id),
                            data = checkStatus.data,
                            userId = "";
                            console.log(checkStatus)
                        //  //获取选中行数量，可作为是否有选中行的条件
                        if (data.length > 0) {
                            for (var i in data) {
                                userId += data[i].id + ",";
                            }
                            console.log(userId);//打印获取到选中的id，用，分割
                            layer.confirm('确定销毁选中的用户？', { icon: 3, title: '提示信息' }, function (index) {
                                $.ajax({
                                    url: "destroy",
                                    type: "POST",
                                    data: { id: userId },
                                    success: function (msg) {
                                        if (msg.errcode == "1") {
                                            layer.msg('销毁成功!', function () { time: 2000 })
                                            table.reload('current', {
                                                scrollPos: 'fixed'
                                            });
                                        } else {
                                            layer.msg("销毁失败", function () { time: 2000 });
                                        }
                                    }
                                });
                            });
                        } else {
                            layer.msg("请选择需要销毁的用户");  //如果未选择数据，则提示。
                        }
                    }
                })
            } else {
                $("#destroy").addClass("layui-btn-disabled")
                $("#restore").addClass("layui-btn-disabled")

            }
        })

    });

</script>